<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class AvatarController extends Controller
{
    public function index(Request $request)
    {
        $name = $request->input('name', $this->getNickname((string)rand(00, 99)));
        $avatarService = new \Multiavatar();
        $svg = $avatarService->generate($name, null, null);
        return view('web.avatar', [
            'svg' => $svg,
            'name' => $name
        ]);
    }

    function getNickname($id)
    {
        $nickname = '';
        $arr = array('富', '强', '民', '主', '文', '明', '和', '谐', '自', '平', '等');
        for ($i = 0; $i < strlen($id); $i++) {
            $randStr = explode('.', '由.平.等.公.正.法.治.爱.国.敬.业.诚.信.友.善')[rand(0, 14)];
            $nickname .= $arr[$id[$i]] . $randStr;
        }
        return $nickname;
    }
}
